package lanqiao13;

import java.util.Arrays;
import java.util.Scanner;

import com.sun.org.apache.regexp.internal.recompile;


public class F {
	private static int n;
	private static int[][] B;
	private static boolean[] judge;
	private static long count = 0;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		B = new int[3][n];
		for (int i = 0; i < n; i++) {
			B[0][i] = sc.nextInt();
		}
		range(0);
		sc.close();
		
		judge = new boolean[n];
		Arrays.fill(judge, false);
		
		for (int i = 0; i < n; i++) {
			if (i == 0) {
				if (B[1][n - 1] < B[0][i] && B[1][0] < B[0][i]) {
					B[2][1] = B[0][i];
					judge[1] = true;
				}
				if (B[1][1] < B[0][i] && B[1][0] < B[0][i]) {
					B[2][n - 1] = B[0][i];
					judge[n - 1] = true;
				}
				if (B[1][1] < B[0][i] && B[1][n - 1] < B[0][i]) {
					B[2][0] = B[0][i];
					judge[0] = true;
				}
				continue;
			}
			if (i == n - 1) {
				if (B[1][n - 2] < B[0][i] && B[1][n - 1] < B[0][i]) {
					B[2][0] = B[0][i];
					judge[0] = true;
				}
				if (B[1][n - 1] < B[0][i] && B[1][0] < B[0][i]) {
					B[2][n - 2] = B[0][i];
					judge[n - 2] = true;
				}
				if (B[1][n - 2] < B[0][i] && B[1][0] < B[0][i]) {
					B[2][n - 1] = B[0][i];
					judge[n - 1] = true;
				}
				continue;
			}
			
			if (B[1][i - 1] < B[0][i] && B[1][i] < B[0][i]) {
				B[2][i + 1] = B[0][i];
				judge[i + 1] = true;
			}
			if (B[1][i] < B[0][i] && B[1][i + 1] < B[0][i]) {
				B[2][i - 1] = B[0][i];
				judge[i - 1] = true;
			}
			if (B[1][i - 1] < B[0][i] && B[1][i + 1] < B[0][i]) {
				B[2][i] = B[0][i];
				judge[i] = true;
			}
		}
		
		dfs(0);
		System.out.println(count % 1000000007l);
	}
	
	private static void range(int min) {
		for (int i = 0; i < n; i++) {
			if (i == 0) {
				min = Math.min(B[0][n - 1], B[0][0]);
				min = Math.min(min, B[0][1]);
				B[1][0] = min;
				continue;
			} else if (i == n - 1) {
				min = Math.min(B[0][n - 2], B[0][n - 1]);
				min = Math.min(min, B[0][0]);
				B[1][n - 1] = min;
				continue;
			} else {
				min = Math.min(B[0][i - 1], B[0][i]);
				min = Math.min(min, B[0][i + 1]);
				B[1][i] = min;
			}
		}
	}
	
	private static void dfs(int pos) {
		if (pos == n) {
			if (f1()) {
				count++;
			}
			return;
		}
		if (judge[pos]) {
			dfs(pos + 1);
			return;
		}
		
		for (int i = 0; i <= B[1][pos]; i++) {
			B[2][pos] = i;
			dfs(pos + 1);
		}
	}
	
	private static boolean f1() {
		if (Math.max(Math.max(B[2][n - 1], B[2][0]), B[2][1]) != B[0][0]) {
			return false;
		}
		if (Math.max(Math.max(B[2][n - 2], B[2][n - 1]), B[2][0]) != B[0][n - 1]) {
			return false;
		}
		for (int i = 2; i < n - 1; i++) {
			if (Math.max(Math.max(B[2][i - 1], B[2][i]), B[2][i + 1]) != B[0][i]) {
				return false;
			}
		}
		return true;
	}
}
